<?php
//信息分类控制器
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Org\Upload;

class TypeController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = \DB::select("select * from classone order by concat(pidstr,id) asc");
        
        //处理信息
        foreach($list as &$v){
            $m = substr_count($v->pidstr,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
        return view("admin.type.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

		$list = \DB::select("select * from classone order by concat(pidstr,id) asc");
		
		//处理信息
        foreach($list as &$v){
            $m = substr_count($v->pidstr,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
        return view("admin.type.create",['list'=>$list]);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		if(empty($_POST['name'])){
			$info = "输入信息不完整！";
			return view("admin.type.info",['info'=>$info]);
		}
		
		//获取要添加的数据
		
		$pid = $_POST['pid'];
		$name = $_POST['name'];
		
		if($_POST['pid']==0){
			$pidstr = "0,";
		}else{
			$str = \DB::table('classone')->where('id',$pid)->value("pidstr");
			
			$pidstr = $str.$_POST['pid'].",";	
		}
			//执行添加
			$id = \DB::table("classone")->insertGetId(['name'=>$name,'pid'=>$pid,'pidstr'=>$pidstr]);
        //判断
        if($id>0){
            $info = "类别信息添加成功！";
        }else{
            $info = "类别信息添加失败！";
        }
       
         return redirect("admin/type")->with("err",$info);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
       $list = \DB::select("select * from classone order by concat(pidstr,id) asc");
	   //处理信息
        foreach($list as &$v){
            $m = substr_count($v->pidstr,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
	   $data = \DB::table("classone")->where('id',$id)->first();
	   return view("admin.type.edit",['list'=>$list,'data'=>$data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
		if($_FILES['myfile']['error']==4){	
			$pid = $_POST['pid'];
			$name = $_POST['name'];
			
			if($_POST['pid']==0){
				$pidstr = "0,";
			}else{
				$str = \DB::table('classone')->where('id',$pid)->value("pidstr");
				
				$pidstr = $str.$_POST['pid'].",";	
			}
			//执行修改
			$id = \DB::table("classone")->where("id",$id)->update(['name'=>$name,'pid'=>$pid,'pidstr'=>$pidstr]);
			
			//判断
			if($id>0){
				$info = "类别信息修改成功！";
			}else{
				$info = "类别信息修改失败！";
			}
			return redirect("admin/type")->with("err",$info);
		}else{
			$upfile = new Upload("myfile");
	
			//初始化上传信息
			$upfile->path="./img/home/"; //上传储存路径
			$upfile->typeList =array("image/jpeg","image/png","image/gif"); //设置允许上传类型
			$upfile->maxSize =0; //允许上传大小
	
			//执行文件上传
			$res = $upfile->doUpload();
	
			//判断输出
			if($res){
				//执行数据库添加
				$pic = $upfile->saveName;
				$pid = $_POST['pid'];
				$name = $_POST['name'];
		
				if($_POST['pid']==0){
					$pidstr = "0,";
					//执行修改
					$id = \DB::table("classone")->where("id",$id)->update(['name'=>$name,'pid'=>$pid,'pic'=>$pic]);
		
					//判断
					if($id>0){
						$info = "类别信息修改成功！";
					}else{
						$info = "类别信息修改失败！";
					}
					return redirect("admin/type")->with("err",$info);
				}else{
					$str = \DB::table('classone')->where('id',$pid)->value("pidstr");

					$pidstr = $str.$_POST['pid'].",";
					//执行修改
					$id = \DB::table("classone")->where("id",$id)->update(['name'=>$name,'pid'=>$pid,'pidstr'=>$pidstr,'pic'=>$pic]);
		
					//判断
					if($id>0){
						$info = "类别信息修改成功！";
					}else{
						$info = "类别信息修改失败！";
					}
					return redirect("admin/type")->with("err",$info);
				}
			}else{
				$info = "上传失败！原因：".$upfile->error;
				return view("admin.type.info",['info'=>$info]);
			}
		}
	}
    public function destroy($id)
    {
		//判断是否含有子类
		$num = \DB::table('classone')->where('pid',$id)->count();
		if($num>=1){
			$info = "含有子类的分类不允许删除！";
			return view("admin.type.info",['info'=>$info]);
		}else{
			\DB::table('classone')->delete($id);
			return redirect("admin/type");
		}
    }
	
	public function checkstatus(){
		$fid =  $_REQUEST['fid'];
		
		$m = \DB::table('classone')->where('id',$fid)->pluck('status');
		if($m==1){
			\DB::table('classone')->where('id',$fid)->update('status',0);
			return "未启用分类";
		}else{
			\DB::table('classone')->where('id',$fid)->update('status',1);
			return "已启用";
		}
	}
}
